

#include "StdSqlite.h"

#define true 1
#define false 0
struct StdSqlite
{
    sqlite3 *db;
};

Sql *InitSqlite(const char *filename)
{
    Sql* s = (Sql*)malloc(sizeof(Sql));
    if( s == NULL)
    {
        printf("InitSqlite malloc error!\n");
        return NULL ;
    }
    if(sqlite3_open(filename,&s->db) != SQLITE_OK)
    {
        printf("database file open file !\n");
        free(s);
        return NULL ;
    }


    return s;
}

void CreateTable(Sql *s, const char *TableName, char **PropertyList, int row)
{
    char proList[1024];
    for(int i = 0 ; i < row ; i ++)
    {
        char property[100] = {0};
        if(strcmp(PropertyList[i*2],"id") == 0)
        {
            sprintf(property,"%s %s primary key", PropertyList[i*2],PropertyList[i*2+1]);
        }
        else
        {
            sprintf(property,"%s %s", PropertyList[i*2],PropertyList[i*2+1]);
        }
        strncat(proList,property,strlen(property));
        if(i != row-1)
            strcat(proList,",");
    }

    char sql[4096] = {0};
    sprintf(sql,"create table %s(%s);",TableName,proList);
    
    if( sqlite3_exec(s->db,sql,NULL,NULL,NULL) != SQLITE_OK)
    {
        printf("create table error: %s\n",sqlite3_errmsg(s->db));
    }
}

void InsertData(Sql *s, const char *TableName, char **values, int num)
{
    char str[1000] = {0};
    for(int i = 0 ; i < num ; i ++)
    {
        strcat(str,values[i]);
        if(i != num-1)
            strcat(str,",");
    }    
    char sql[4096] = {0};
    sprintf(sql,"insert into %s  values(%s);",TableName,str);
    
    if( sqlite3_exec(s->db,sql,NULL,NULL,NULL) != SQLITE_OK)
    {
        printf("insert table error: %s",sqlite3_errmsg(s->db));
    }
}

void UpdateData(Sql *s, const char *TableName, const char *UpdateStr, const char *where)
{
    char sql[4096] = {0};
    sprintf(sql,"update %s set %s where %s;",TableName,UpdateStr,where);
    
    if( sqlite3_exec(s->db,sql,NULL,NULL,NULL) != SQLITE_OK)
    {
        printf("insert table error: %s",sqlite3_errmsg(s->db));
    }
}

void DeleteData(Sql *s, const char *TableName, const char *where)
{
    char sql[4096] = {0};

    if(where == NULL)
    {
        sprintf(sql,"delete from %s;",TableName);
    }
    else
    {
        sprintf(sql,"delete from %s where %s;",TableName,where);
    }
    if( sqlite3_exec(s->db,sql,NULL,NULL,NULL) != SQLITE_OK)
    {
        printf("deletedata error: %s",sqlite3_errmsg(s->db));
    }
}

void GetAllFromTable(Sql *s, const char *TableName, char ***result, int *row, int *column)
{
    char sql[400] = {0} ;
    sprintf(sql,"select *from %s;",TableName);
    
    if( sqlite3_get_table(s->db,sql,result,row,column,NULL) != SQLITE_OK)
    {
        printf("get table error : %s\n",sqlite3_errmsg(s->db));
        return;
    }

}

char* GetIDFromTableINRes(Sql *s, const char *TableName,char* Name,char* passwd )
{
    char sql[400] = {0} ;
    sprintf(sql,"select *from %s where name = '%s' and passwd='%s';",TableName,Name,passwd);
    char **result;
    int row,column;
    if( sqlite3_get_table(s->db,sql,&result,&row,&column,NULL) != SQLITE_OK)
    {
        printf(" error: %s",sqlite3_errmsg(s->db));
        return NULL ;
    }
    
    return result[row*3];
}

int ConfirmById(Sql* s,const char *TableName,const char* id,const char* Name,const char* passwd )
{
    char sql[400] = {0} ;
    sprintf(sql,"select *from %s where id=%s ;",TableName,id);
    char **result;
    int row,column;
    if( sqlite3_get_table(s->db,sql,&result,&row,&column,NULL) != SQLITE_OK)
    {
        printf(" error: %s",sqlite3_errmsg(s->db));
        return -1;
    }
    if(strcmp(result[4],Name) == 0 && strcmp(result[5],passwd) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
    sqlite3_free_table(result);


    sqlite3_close(s->db);
}

void FreeResult(Sql *s, char **result)
{
    sqlite3_free_table(result);
}

int GetTableLen(Sql *s, const char *TableName)
{
    char sql[4096] = {0};
    sprintf(sql,"select count(*) from %s;",TableName);
    char **result;
    int row,column;
    if( sqlite3_get_table(s->db,sql,&result,&row,&column,NULL) != SQLITE_OK)
    {
        printf(" error: %s",sqlite3_errmsg(s->db));
        return -1 ;
    }
    int count = atoi(result[column]);
    sqlite3_free_table(result);
    return count;
}
